package com.hxci.hospital.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;

public class AuthInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 获取请求的URL
        String url = request.getRequestURI();

        // 公开访问的URL，不需要登录
        if(url.contains("/patient/") || url.contains("/doctor/login") || url.contains("/doctor/tologin") || 
           url.contains("/ai/") || url.contains("/js/") || url.contains("/css/") || 
           url.equals("/hospital/") || url.equals("/hospital/index")) {
            return true;
        }

        // 获取session
        HttpSession session = request.getSession();
        Object doctor = session.getAttribute("doctor");

        // 判断医生是否已登录
        if(doctor != null) {
            return true;
        }

        // 没有登录，重定向到登录页面
        response.sendRedirect("/hospital/doctor/login");
        return false;
    }
}
